<?php


namespace app\ymsh\controller\java;


use app\common\service\finance\merchant\BalanceService;
use app\model\AgentMerchantStat;
use think\Exception;
use think\facade\Db;
use think\facade\Log;

class Finance  extends BaseController
{
    /**
     * 月份统计
     */
    public function statistical(){

        $msg = dmjValidate([
            'merchant_id' => 'require'
        ],[],request()->all());

        if ($msg) return fail($msg);

        $merchant_id = input('merchant_id');

        //先把相关佣金日期统计成上个月的，顺延


        try {
            Db::startTrans();

            //先统计
            (new BalanceService())->statByMerchantId($merchant_id);

            //再从数据库拉取
            $agentMerchantStat = AgentMerchantStat::with('agentMerchantStatItem')->where('merchant_id',$merchant_id)
                ->where('status',AgentMerchantStat::STATUS_WAIT)
                ->select();

            Db::commit();

            return success('',$agentMerchantStat);


        }catch (Exception $exception){
            Db::rollback();
            Log::error($exception->getMessage());
            Log::error($exception->getFile());
            Log::error($exception->getLine());
            return fail();
        }




    }








}